package com.ks.core.framework.cpsupload.demos.pay;

import java.util.Map;

// 4. 抽象支付策略（核心模板方法）
public abstract class PaymentStrategy {

    // 模板方法：执行支付操作
    public final UnifiedResponse executeOperation( PaymentOperation operation, Map<String, Object> params ) {
        try {
            // 1. 参数校验
            validateParams(operation, params);

            // 2. 执行具体操作
            Map<String, Object> rawResponse = doOperation(operation, params);

            // 3. 统一响应转换
            return buildUnifiedResponse(operation, rawResponse);
        } catch (IllegalArgumentException ex) {
            return UnifiedResponse.fail("PARAM_ERROR", ex.getMessage());
        } catch (Exception ex) {
            return UnifiedResponse.fail("SYSTEM_ERROR", "支付系统异常: " + ex.getMessage());
        }
    }

    // 抽象方法：具体支付操作实现
    protected abstract Map<String, Object> doOperation( PaymentOperation operation, Map<String, Object> params ) throws Exception;

    // 抽象方法：参数校验
    protected abstract void validateParams( PaymentOperation operation, Map<String, Object> params ) throws IllegalArgumentException;

    // 抽象方法：构建统一响应
    protected abstract UnifiedResponse buildUnifiedResponse( PaymentOperation operation, Map<String, Object> rawResponse );

    // 回调处理（单独拆分，异步场景）
    public abstract UnifiedResponse handleCallback( Map<String, String> callbackParams );
}